{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python395jvsc74a57bd0d21333ff62c43b8d3f22f2fffa3b7389f580599dfc1ab4fc74fc1532be50abca",
   "display_name": "Python 3.9.5 64-bit"
  },
  "metadata": {
   "interpreter": {
    "hash": "d21333ff62c43b8d3f22f2fffa3b7389f580599dfc1ab4fc74fc1532be50abca"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "30\n5\n"
     ]
    }
   ],
   "source": [
    "def myFn(x):\n",
    "    # 参数检查，必须是int或者float,否则报错\n",
    "    if not isinstance(x,(int,float)):\n",
    "        raise TypeError(\"Bad operand type\")\n",
    "    if x >= 0:\n",
    "        return x\n",
    "    else:\n",
    "        return -x\n",
    "\n",
    "print(myFn(-30))\n",
    "print(myFn(5))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "151.96152422706632 70.0\n<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "# 函数返回多个返回值\n",
    "import math\n",
    "def move(x,y,step,angle=0):\n",
    "    nx = x+step * math.cos(angle)\n",
    "    ny = y - step * math.sin(angle)\n",
    "    return nx,ny\n",
    "\n",
    "x,y = move(100,100,60,math.pi/6)   \n",
    "print(x,y) \n",
    "r = move(100,100,60,math.pi/6) \n",
    "print( type(r) )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "25\n1024\n"
     ]
    }
   ],
   "source": [
    "# 函数的默认参数\n",
    "#默认参数，就是给参数默认值，不传值就调用默认值\n",
    "def newPower(x,n=2):\n",
    "    s = 1\n",
    "    while n > 0:\n",
    "        n = n-1\n",
    "        s = s * x\n",
    "    return s\n",
    "resault = newPower(5)\n",
    "r1 = newPower(2,10)\n",
    "print(resault)      \n",
    "print(r1)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "385\n385\n"
     ]
    }
   ],
   "source": [
    "# 可变参数\n",
    "def cale(numbers):\n",
    "    sum = 0\n",
    "    for n in numbers:\n",
    "        sum = sum + n*n\n",
    "    return sum\n",
    "#  传一个list:    \n",
    "res = cale([1,2,3,4,5,6,7,8,9,10])\n",
    "print(res)\n",
    "# 传N个单独的数字呢？\n",
    "#res = cale(1,2,3) # TypeError: cale() takes 1 positional argument but 3 were given\n",
    "print(res) \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "14\n"
     ]
    }
   ],
   "source": [
    "# 可变参数：在参数前加 *\n",
    "def cale(*numbers):\n",
    "    sum = 0\n",
    "    for n in numbers:\n",
    "        sum = sum + n*n\n",
    "    return sum\n",
    "\n",
    "res = cale(1,2,3) \n",
    "print(res) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "TypeError",
     "evalue": "can't multiply sequence by non-int of type 'list'",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-25-a8fe84886999>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      6\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0msum\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0mres\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcale\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      9\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mres\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-25-a8fe84886999>\u001b[0m in \u001b[0;36mcale\u001b[1;34m(*numbers)\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[0msum\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mn\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mnumbers\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m         \u001b[0msum\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msum\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mn\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      6\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0msum\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'list'"
     ]
    }
   ],
   "source": [
    "# 当使用了可变参数之后，再给他传list：\n",
    "def cale(*numbers):\n",
    "    sum = 0\n",
    "    for n in numbers:\n",
    "        sum = sum + n*n\n",
    "    return sum\n",
    "\n",
    "res = cale([1,2,3]) \n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "14\n"
     ]
    }
   ],
   "source": [
    "# 给有可变参数的函数传list正确的姿势：\n",
    "def cale(*numbers):\n",
    "    sum = 0\n",
    "    for n in numbers:\n",
    "        sum = sum + n*n\n",
    "    return sum\n",
    "# ★★★★记住：在[]前加 * ：\n",
    "res = cale(*[1,2,3]) \n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "14\n"
     ]
    }
   ],
   "source": [
    "#那传tuple呢？\n",
    "def cale(*numbers):\n",
    "    sum = 0\n",
    "    for n in numbers:\n",
    "        sum = sum + n*n\n",
    "    return sum\n",
    "# tuple前面加* 准确无误！\n",
    "res = cale(*(1,2,3)) \n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关键字参数\n",
    "def person()"
   ]
  }
 ]
}